<?php
namespace Rate\Controller;
use Todo\Controller\ApiController;

class UserController extends ApiController {
    protected $_slistField = 'user_id id, name title';

    //过滤查询条件
    protected function _filter(&$where){
        $keywords = I('request.keywords/s');
        if($keywords) $where['name'] = array('like', '%'.$keywords.'%');
    }

    /**
     * 删除项目和用户的关联关系
     */
    public function del(){
        $proId = I('request.project_id/d');
        $userId = I('request.user_id/d');
        if(!$proId || !$userId) $this->apiError("非法请求!");

        $this->_loadModel();
        if($this->_model->where(array('user_id'=>$userId,'project_id'=>$proId))->delete() === false){
            $this->apiError("移除用户失败:".$this->_model->getError());
        }else{
            $this->apiSuccess("移除成功!");
        }
    }


    public function my(){
        $this->_loadModel();
        $this->_model->alias('t')->JOIN('INNER JOIN rate_project p ON p.id=t.project_id')->field('p.id,p.title');

        $where = array('t.user_id'=>$this->_user['id']);
        if(I('get.type') == '1'){ //被评
            $where['t.type'] = '1';
            $where['p.end_time'] = array('lt', NOW_TIME);
            $this->_model->JOIN('INNER JOIN rate_results r ON r.project_id=t.project_id AND r.user_id=t.user_id')->field('p.id,p.title,r.people,r.score,r.sort');
        }else{ //参评
            $where['t.type'] = '2';
            $where['p.start_time'] = array('lt', NOW_TIME);
            $where['p.end_time'] = array('gt', NOW_TIME);
        }
        $where['p.status'] = '1';
        $list =  $this->_model->where($where)->order('t.project_id DESC')->select();
        $this->apiList(array('list'=>$list, 'total'=>count($list)));
    }


    public function _before_imports(){
        $proId = I('post.project_id/d');
        if(!$proId) $this->apiError("必须选择项目!");

        $userType = I('post.user_type');
        if(!$userType)  $this->apiError("必须选择用户类型!");

        $type = I('post.type');
        if(!$type)  $this->apiError("必须选择参与类型!");
    }
    
}